﻿@page "/website/add"
@using System.Text
@using BPanel.Database.Enums
@attribute [StreamRendering]

<PageTitle>新增站点</PageTitle>

<div class="bg-white p-5">
    <Form Model="@model" LabelColSpan="3">
        <FormItem Label="站点名称">
            <Input Placeholder="站点名称" @bind-Value="@context.Name" Width="300"></Input>
        </FormItem>
        <FormItem Label="域名列表" WrapperColSpan="12">
            <TextArea Placeholder="域名列表，每一行一个，多个请换行" Rows="5" MaxRows="5" @bind-Value="@context.Domains" Width="300" AutoSize="false"></TextArea>
        </FormItem>
        <FormItem Label="根目录">
            <Input Placeholder="根目录" @bind-Value="@context.RootDirectory" Width="300"></Input>
        </FormItem>
        <FormItem Label="备注" WrapperColSpan="12">
            <TextArea Placeholder="备注" Rows="5" MinRows="5" @bind-Value="@context.Description" Width="300" AutoSize="@(false)"></TextArea>
        </FormItem>
        <FormItem Label="FTP">
            <RadioGroup @bind-Value="@context.CreateFtp" Size="large">
                <Radio RadioButton Value="@(true)">创建</Radio>
                <Radio RadioButton Value="@(false)">不创建</Radio>
            </RadioGroup>
        </FormItem>
        @if (context.CreateFtp)
        {
            <FormItem WrapperColOffset="3" WrapperColSpan="21">
                <span>FTP账号：</span>
                <AntDesign.Input Placeholder="FTP账号" @bind-Value="@context.FtpUsername" Width="160">
                    <AddOnAfter>
                        <Button Type="@ButtonType.Primary" Style="width: auto;" OnClick="BuildFtpUsername">
                            <Icon Type="sync" Theme="outline"/>
                        </Button>
                    </AddOnAfter>
                </AntDesign.Input>
            </FormItem>
            <FormItem WrapperColOffset="3" WrapperColSpan="21">
                <span>FTP密码：</span>
                <AntDesign.Input Placeholder="FTP密码" @bind-Value="@context.FtpPassword" Width="160">
                    <AddOnAfter>
                        <Button Type="@ButtonType.Primary" Style="width: auto;" OnClick="BuildFtpPassword">
                            <Icon Type="sync" Theme="outline"/>
                        </Button>
                    </AddOnAfter>
                </AntDesign.Input>
            </FormItem>
        }
        <FormItem Label="数据库">
            <RadioGroup @bind-Value="@context.CreateDatabase" Size="large">
                <Radio RadioButton Value="@(true)">创建</Radio>
                <Radio RadioButton Value="@(false)">不创建</Radio>
            </RadioGroup>
        </FormItem>
        @if (context.CreateDatabase)
        {
            <FormItem WrapperColOffset="3" WrapperColSpan="21">
                <span>数据库账号：</span>
                <AntDesign.Input Placeholder="数据库账号" @bind-Value="@context.DatabaseUsername" Width="160">
                    <AddOnAfter>
                        <Button Type="@ButtonType.Primary" Style="width: auto;" OnClick="BuildDatabaseUsername">
                            <Icon Type="sync" Theme="outline"/>
                        </Button>
                    </AddOnAfter>
                </AntDesign.Input>
            </FormItem>
            <FormItem WrapperColOffset="3" WrapperColSpan="21">
                <span>数据库密码：</span>
                <AntDesign.Input Placeholder="数据库密码" @bind-Value="@context.DatabasePassword" Width="160">
                    <AddOnAfter>
                        <Button Type="@ButtonType.Primary" Style="width: auto;" OnClick="BuildDatabasePassword">
                            <Icon Type="sync" Theme="outline"/>
                        </Button>
                    </AddOnAfter>
                </AntDesign.Input>
            </FormItem>
        }
        <FormItem WrapperColOffset="3" WrapperColSpan="21">
            <Button Type="@ButtonType.Primary">提交</Button>
        </FormItem>
    </Form>
</div>

<style>
    .ant-input-group-addon {
        padding: 0;
        border: 0;
    }
</style>

@code{

    public class Model
    {
        /// <summary>
        /// 网站名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 站点类型
        /// </summary>
        public EnumSiteType SiteType { get; set; }

        /// <summary>
        /// Php版本
        /// </summary>
        public EnumPhpVersion? PhpVersion { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 根目录
        /// </summary>
        public string RootDirectory { get; set; }

        /// <summary>
        /// 默认端口
        /// </summary>
        public int Port { get; set; } = 80;

        /// <summary>
        /// 域名列表
        /// </summary>
        public string Domains { get; set; }

        /// <summary>
        /// 协议
        /// </summary>
        public EnumProtocol Protocol { get; set; } = EnumProtocol.Http;

        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime? ExpiredTime { get; set; }

        /// <summary>
        /// 是否创建FTP
        /// </summary>
        public bool CreateFtp { get; set; }

        /// <summary>
        /// FTP账号
        /// </summary>
        public string FtpUsername { get; set; }

        /// <summary>
        /// FTP密码
        /// </summary>
        public string FtpPassword { get; set; }

        /// <summary>
        /// 是否创建数据库
        /// </summary>
        public bool CreateDatabase { get; set; }

        /// <summary>
        /// 数据库账号
        /// </summary>
        public string DatabaseUsername { get; set; }

        /// <summary>
        /// 数据库密码
        /// </summary>
        public string DatabasePassword { get; set; }
    }

    private Model model = new Model();

    private Random random = new Random();

    string GetRandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder result = new StringBuilder(12);
        for (int i = 0; i < length; i++)
        {
            result.Append(chars[random.Next(chars.Length)]);
        }

        return result.ToString();
    }

    void BuildFtpUsername()
    {
        model.FtpUsername = GetRandomString(12);
    }

    void BuildFtpPassword()
    {
        model.FtpPassword = GetRandomString(12);
    }

    void BuildDatabaseUsername()
    {
        model.DatabaseUsername = GetRandomString(12);
    }

    void BuildDatabasePassword()
    {
        model.DatabasePassword = GetRandomString(12);
    }

}